package edu.school.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.school.entity.Adopt;
import edu.school.entity.AdoptExample;
import edu.school.entity.Goods;
import edu.school.entity.User;
import edu.school.mapper.AdoptMapper;
import edu.school.mapper.GoodsMapper;
import edu.school.mapper.UserMapper;
import edu.school.service.AdoptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdoptServiceImpl implements AdoptService {
    @Autowired
    AdoptMapper mapper;
    @Autowired
    UserMapper userMapper;
    @Autowired
    GoodsMapper goodsMapper;
    @Override
    public boolean save(Adopt adopt) {
        return mapper.insertSelective(adopt)>0?true:false;
    }

    @Override
    public boolean update(Adopt adopt) {
        return mapper.updateByPrimaryKeySelective(adopt)>0?true:false;
    }

    @Override
    public PageInfo<Adopt> getPageList(Integer pageNum, Integer pageSize, Adopt record) {
        AdoptExample example = new AdoptExample();
        example.setOrderByClause("create_time desc");
        AdoptExample.Criteria criteria = example.createCriteria();
        if (null != record) {
//            if (StringUtils.isNoneBlank(record.getTitle())) {//公告标题查询
//                criteria.andTitleLike("%" + record.getTitle() + "%");
//            }
             if (record.getUid()!=null) {//用户id查询
                criteria.andUidEqualTo(record.getUid());
            }
             if (record.getU_id() !=null) {//用户id查询
                criteria.andU_idEqualTo(record.getU_id());
            }
             if (record.getState()!=null) {//用户id查询
                criteria.andStateEqualTo(record.getState());
            }
        }
        PageHelper.startPage(pageNum, pageSize);//第1步
        List<Adopt> list = mapper.selectByExample(example);//第2步
        for(Adopt data:list){
            User user=userMapper.selectByPrimaryKey(data.getUid());//认领人信息
            User user1=userMapper.selectByPrimaryKey(data.getU_id());//拾物者信息
            Goods goods=goodsMapper.selectByPrimaryKey(data.getGid());
            data.setUser(user);
            data.setUser1(user1);
            data.setGoods(goods);
        }
        PageInfo<Adopt> info = new PageInfo<>(list);
        info.setPageNum(info.getPageNum());
        info.setPages(info.getPages());
        return info;

    }

    @Override
    public Adopt findById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public boolean deleteAdopt(Integer id) {
        return mapper.deleteByPrimaryKey(id)>0?true:false;
    }

    @Override
    public int deleteMany(String[] ids) {
        return mapper.deleteMany(ids);
    }
}
